﻿// <copyright file="TypeExtensions.cs" company="N/A">
//     Copyright (c) Nicholas Barrett and Benjamin Kasten.  All rights reserved.
//
//     This program is free software; you can redistribute it and/or modify it
//     under the terms of the GNU General Public License as published by the
//     Free Software Foundation; either version 2 of the License, or (at your
//     option) any later version.
//
//     This program is distributed in the hope that it will be useful, but
//     WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
//     Public License for more details.
//
//     You should have received a copy of the GNU General Public License along
//     with this program; if not, write to the Free Software Foundation, 
//     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// </copyright>

namespace BetterAzureTableStorage
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;

    /// <summary>
    /// Type Extensions
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        /// Gets or sets a dictionary of internal types and their EDM resolutions.
        /// </summary>
        private static Dictionary<Type, string> EdmTypes = new Dictionary<Type, string>()
            {
                { typeof(int), "Edm.Int32" },
                { typeof(long), "Edm.Int64" },
                { typeof(string), "Edm.String" },
                { typeof(double), "Edm.Double" },
                { typeof(float), "Edm.Float" },
                { typeof(decimal), "Edm.Decimal" },
                { typeof(DateTime), "Edm.DateTime" },
                { typeof(Guid), "Edm.Guid" },
                { typeof(bool), "Edm.Boolean" },
                { typeof(byte[]), "Edm.Binary" },
                { typeof(Uri), "Edm.String" }
            };

        /// <summary>
        /// Gets the type as it is known by the entity data model.
        /// </summary>
        /// <param name="self">The reflected type to get the edm type of.</param>
        /// <returns>The edm type as a string.</returns>
        public static string GetEdmType(this Type self)
        {
            if (self.IsEnum)
            {
                return EdmTypes[typeof(int)];
            }
            else
            {
                return EdmTypes[self];
            }
        }

        /// <summary>
        /// Gets the element type of a generic enumerable.
        /// </summary>
        /// <param name="type">The type to analyze.</param>
        /// <returns>Returns the first generic argument of an IEnumerable type, or <paramref name="type"/></returns>
        public static Type GetEffectiveType(this Type type)
        {
            if (typeof(IEnumerable).IsAssignableFrom(type) && type.IsGenericType)
            {
                return type.GetGenericArguments()[0];
            }
            else
            {
                return type;
            }
        }
    }
}
